﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Text;

namespace yule.CodeBuilder
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class Global : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new ExceptionHandleAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

        }
    }







    /// <summary>
    /// 异常过滤器
    /// </summary>
    public class ExceptionHandleAttribute : IExceptionFilter
    {
        #region IExceptionFilter 成员

        public void OnException(ExceptionContext filterContext)
        {
            //Mvc.Controller 异常处理

            Exception ex = filterContext.Exception;

            System.Text.StringBuilder sb = new StringBuilder();

            sb.AppendLine("");
            sb.AppendLine("***************************************************************");

            sb.AppendLine(string.Format("Time      :{0}", System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")));
            sb.AppendLine(string.Format("Controller:{0}", filterContext.Controller.GetType().Name));
            sb.AppendLine(string.Format("Message   :{0}", ex.Message));
            sb.AppendLine(string.Format("Source    :{0}", ex.Source));
            sb.AppendLine(string.Format("StackTrace:{0}", ex.StackTrace));

            sb.AppendLine("***************************************************************");

        }

        #endregion
    }

}